home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / sunbird / components / calendarService.js < prev    next >
Encoding:
Text File  |  2007-05-23  |  12.0 KB  |  357 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1999
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Seth Spitzer <sspitzer@netscape.com>
  24.  *   Robert Ginda <rginda@netscape.com>
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. /*
  41.  * This file contains the following calendar related components:
  42.  * 1. Command line handler service, for responding to the -webcal command line
  43.  *    option. (CLineHandler)
  44.  * 2. Content handler for responding to content of type text/calendar
  45.  *    (ICALContentHandler)
  46.  * 3. Protocol handler for supplying a channel to the browser when an webcal://
  47.  *    link is clicked. (ICALProtocolHandler)
  48.  * 4. A (nearly empty) imeplementation of nsIChannel for telling the browser
  49.  *    that webcal:// links have the content type text/calendar (BogusChannel)
  50.  */
  51.  
  52. /* components defined in this file */
  53. const CLINE_SERVICE_CONTRACTID =
  54.     "@mozilla.org/commandlinehandler/general-startup;1?type=calendar";
  55. const CLINE_SERVICE_CID =
  56.     Components.ID("{65ef4b0b-d116-4b93-bf8a-84525992bf27}");
  57. const ICALCNT_HANDLER_CONTRACTID =
  58.     "@mozilla.org/uriloader/content-handler;1?type=text/calendar";
  59. const ICALCNT_HANDLER_CID =
  60.     Components.ID("{9ebf4c8a-7770-40a6-aeed-e1738129535a}");
  61. const ICALPROT_HANDLER_CID =
  62.     Components.ID("{d320ba05-88cf-44a6-b718-87a72ef05918}");
  63.  
  64. /* components used in this file */
  65. const MEDIATOR_CONTRACTID =
  66.     "@mozilla.org/appshell/window-mediator;1";
  67. const STANDARDURL_CONTRACTID =
  68.     "@mozilla.org/network/standard-url;1";
  69. const ASS_CONTRACTID =
  70.     "@mozilla.org/appshell/appShellService;1";
  71. const WINDOWWATCHER_CONTRACTID =
  72.     "@mozilla.org/embedcomp/window-watcher;1";
  73.  
  74. /* interafces used in this file */
  75. const nsIWindowMediator  = Components.interfaces.nsIWindowMediator;
  76. const nsICmdLineHandler  = Components.interfaces.nsICmdLineHandler;
  77. const nsICategoryManager = Components.interfaces.nsICategoryManager;
  78. const nsIContentHandler  = Components.interfaces.nsIContentHandler;
  79. const nsIFactory         = Components.interfaces.nsIFactory;
  80. const nsIProtocolHandler = Components.interfaces.nsIProtocolHandler;
  81. const nsIURI             = Components.interfaces.nsIURI;
  82. const nsIStandardURL     = Components.interfaces.nsIStandardURL;
  83. const nsIChannel         = Components.interfaces.nsIChannel;
  84. const nsIRequest         = Components.interfaces.nsIRequest;
  85. const nsIAppShellService = Components.interfaces.nsIAppShellService;
  86. const nsISupports        = Components.interfaces.nsISupports;
  87. const nsIWindowWatcher   = Components.interfaces.nsIWindowWatcher;
  88.  
  89. if ("nsICommandLineHandler" in Components.interfaces)
  90.      const nsICommandLineHandler = Components.interfaces.nsICommandLineHandler;
  91.  
  92. /* Command Line handler service */
  93. function CLineService()
  94. {}
  95.  
  96. CLineService.prototype = {
  97.     /* nsISupports */
  98.     QueryInterface : function service_qi(iid) {
  99.         if (iid.equals(nsISupports))
  100.             return this;
  101.  
  102.         if (nsICmdLineHandler && iid.equals(nsICmdLineHandler))
  103.             return this;
  104.  
  105.         if (nsICommandLineHandler && iid.equals(nsICommandLineHandler))
  106.             return this;
  107.  
  108.         throw Components.results.NS_ERROR_NO_INTERFACE;
  109.     },
  110.  
  111.     /* nsICmdLineHandler */
  112.  
  113.     commandLineArgument : "-calendar",
  114.     prefNameForStartup  : "general.startup.calendar",
  115.     chromeUrlForTask    : "chrome://calendar/content",
  116.     helpText            : "Start with calendar",
  117.     handlesArgs         : false,
  118.     defaultArgs         : "",
  119.     openWindowWithArgs  : true,
  120.  
  121.     /* nsICommandLineHandler */
  122.  
  123.     handle : function service_handle(cmdLine) {
  124.         // just pass all arguments on to the Sunbird window
  125.         wwatch = Components.classes[WINDOWWATCHER_CONTRACTID]
  126.                            .getService(nsIWindowWatcher);
  127.         wwatch.openWindow(null, "chrome://calendar/content/",
  128.                               "_blank", "chrome,dialog=no,all", cmdLine);
  129.         cmdLine.preventDefault = true;
  130.     },
  131.  
  132.     helpInfo : "  -subscribe or -url   Pass in a path pointing to a calendar\n" +
  133.                "                       to subscribe to.\n" +
  134.                "  -showdate            Pass in a value for a javascript date\n" +
  135.                "                       to show this date on startup.\n"
  136. };
  137.  
  138. /* factory for command line handler service (CLineService) */
  139. var CLineFactory = {
  140.     createInstance : function (outer, iid) {
  141.         if (outer != null)
  142.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  143.  
  144.         return new CLineService().QueryInterface(iid);
  145.     }
  146. }
  147.  
  148. /* text/calendar content handler */
  149. function ICALContentHandler ()
  150. {}
  151.  
  152. ICALContentHandler.prototype.QueryInterface =
  153. function (iid) {
  154.  
  155.     if (iid.equals(nsIContentHandler) ||
  156.         iid.equals(nsISupports))
  157.         return this;
  158.  
  159.     Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  160.     return null;
  161. }
  162.  
  163. ICALContentHandler.prototype.handleContent =
  164. function(aContentType, aWindowTarget, aRequest)
  165. {
  166.     var channel = aRequest.QueryInterface(nsIChannel);
  167.  
  168.     // Cancel the request ...
  169.     var uri = channel.URI;
  170.     const NS_BINDING_ABORTED = 0x804b0002 // from nsNetError.h
  171.     aRequest.cancel(NS_BINDING_ABORTED);
  172.  
  173.     // ... Subscribe to the uri ...
  174.     var calendarManager = Components.classes["@mozilla.org/calendar/manager;1"]
  175.                                     .getService(Components.interfaces.calICalendarManager);
  176.  
  177.     var newCalendar = calendarManager.createCalendar('ics', uri);
  178.     calendarManager.registerCalendar(newCalendar);
  179.  
  180.     // XXX Come up with a better name, like the filename or X-WR-CALNAME
  181.     // XXX Also, make up a color
  182.     newCalendar.name = "temp";
  183.  
  184.     // ... and open or focus a calendar window.
  185.     var windowManager = Components.classes[MEDIATOR_CONTRACTID]
  186.                                   .getService(nsIWindowMediator);
  187.  
  188.     var w = windowManager.getMostRecentWindow("calendarMainWindow");
  189.  
  190.     if (w) {
  191.         w.focus();
  192.     } else {
  193.         var ass = Components.classes[ASS_CONTRACTID]
  194.                             .getService(nsIAppShellService);
  195.         w = ass.hiddenDOMWindow;
  196.  
  197.         var args = new Object ();
  198.         args.channel = channel;
  199.         w.openDialog("chrome://calendar/content/calendar.xul", "calendar", "chrome,menubar,resizable,scrollbars,status,toolbar,dialog=no", args);
  200.     }
  201. }
  202.  
  203. /* content handler factory object (ICALContentHandler) */
  204. var ICALContentHandlerFactory = new Object();
  205.  
  206. ICALContentHandlerFactory.createInstance =
  207. function (outer, iid) {
  208.     if (outer != null)
  209.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  210.  
  211.     if (!iid.equals(nsIContentHandler) && !iid.equals(nsISupports))
  212.         throw Components.results.NS_ERROR_INVALID_ARG;
  213.  
  214.     return new ICALContentHandler();
  215. }
  216.  
  217.  
  218. /* bogus webcal channel used by the ICALProtocolHandler */
  219. function BogusChannel (aURI)
  220. {
  221.     this.URI = aURI;
  222.     this.originalURI = aURI;
  223. }
  224.  
  225. BogusChannel.prototype.QueryInterface =
  226. function (iid) {
  227.  
  228.     if (iid.equals(nsIChannel) ||
  229.         iid.equals(nsIRequest) ||
  230.         iid.equals(nsISupports))
  231.         return this;
  232.  
  233.     Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  234.     return null;
  235. }
  236.  
  237. /* nsIChannel */
  238. BogusChannel.prototype.loadAttributes = null;
  239. BogusChannel.prototype.contentType = "text/calendar";
  240. BogusChannel.prototype.contentLength = 0;
  241. BogusChannel.prototype.owner = null;
  242. BogusChannel.prototype.loadGroup = null;
  243. BogusChannel.prototype.notificationCallbacks = null;
  244. BogusChannel.prototype.securityInfo = null;
  245.  
  246. BogusChannel.prototype.open =
  247. BogusChannel.prototype.asyncOpen =
  248. function ()
  249. {
  250.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  251. }
  252.  
  253. BogusChannel.prototype.asyncOpen =
  254. function (observer, ctxt)
  255. {
  256.     observer.onStartRequest (this, ctxt);
  257. }
  258.  
  259. BogusChannel.prototype.asyncRead =
  260. function (listener, ctxt)
  261. {
  262.     return listener.onStartRequest (this, ctxt);
  263. }
  264.  
  265. /* nsIRequest */
  266. BogusChannel.prototype.isPending =
  267. function ()
  268. {
  269.     return true;
  270. }
  271.  
  272. BogusChannel.prototype.status = Components.results.NS_OK;
  273.  
  274. BogusChannel.prototype.cancel =
  275. function (aStatus)
  276. {
  277.     this.status = aStatus;
  278. }
  279.  
  280. BogusChannel.prototype.suspend =
  281. BogusChannel.prototype.resume =
  282. function ()
  283. {
  284.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  285. }
  286.  
  287. var CalendarModule = new Object();
  288.  
  289. CalendarModule.registerSelf =
  290. function (compMgr, fileSpec, location, type)
  291. {
  292.     // dump("*** Registering -calendar handler.\n");
  293.  
  294.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  295.  
  296.     compMgr.registerFactoryLocation(CLINE_SERVICE_CID,
  297.                                     "Calendar CommandLine Service",
  298.                                     CLINE_SERVICE_CONTRACTID,
  299.                                     fileSpec,
  300.                                     location,
  301.                                     type);
  302.  
  303.     var catman = Components.classes["@mozilla.org/categorymanager;1"]
  304.                            .getService(nsICategoryManager);
  305.     catman.addCategoryEntry("command-line-argument-handlers",
  306.                             "calendar command line handler",
  307.                             CLINE_SERVICE_CONTRACTID, true, true);
  308.     catman.addCategoryEntry("command-line-handler", "m-calendar",
  309.                             CLINE_SERVICE_CONTRACTID, true, true);
  310.  
  311.     // dump("*** Registering text/calendar handler.\n");
  312.     compMgr.registerFactoryLocation(ICALCNT_HANDLER_CID,
  313.                                     "Webcal Content Handler",
  314.                                     ICALCNT_HANDLER_CONTRACTID,
  315.                                     fileSpec,
  316.                                     location,
  317.                                     type);
  318. }
  319.  
  320. CalendarModule.unregisterSelf =
  321. function(compMgr, fileSpec, location)
  322. {
  323.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  324.  
  325.     compMgr.unregisterFactoryLocation(CLINE_SERVICE_CID, fileSpec);
  326.     compMgr.unregisterFactoryLocation(ICALCNT_HANDLER_CID, fileSpec);
  327.  
  328.     catman = Components.classes["@mozilla.org/categorymanager;1"]
  329.                        .getService(nsICategoryManager);
  330.     catman.deleteCategoryEntry("command-line-argument-handlers",
  331.                                "calendar command line handler", true);
  332.     catman.deleteCategoryEntry("command-line-handler",
  333.                                "m-calendar", true);
  334. }
  335.  
  336. CalendarModule.getClassObject =
  337. function (compMgr, cid, iid) {
  338.     if (cid.equals(CLINE_SERVICE_CID))
  339.         return CLineFactory;
  340.  
  341.     if (cid.equals(ICALCNT_HANDLER_CID))
  342.         return ICALContentHandlerFactory;
  343.  
  344.     throw Components.results.NS_ERROR_NO_INTERFACE;
  345. }
  346.  
  347. CalendarModule.canUnload =
  348. function(compMgr)
  349. {
  350.     return true;
  351. }
  352.  
  353. /* entrypoint */
  354. function NSGetModule(compMgr, fileSpec) {
  355.     return CalendarModule;
  356. }
  357.